/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// MCCLine.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "MCCLine.h"
#include ".\mccline.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(MCCLine, CStatic)
MCCLine::MCCLine(void)
{
	hTheme = NULL;
}

MCCLine::~MCCLine(void)
{
	if(hTheme!=NULL){
		CloseThemeData(hTheme);
		hTheme = NULL;
	}
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(MCCLine, CStatic)
	ON_WM_PAINT()
	ON_MESSAGE(WM_THEMECHANGED, OnThemeChanged)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

void MCCLine::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	COLORREF hColor;
	COLORREF sColor;
	CPen pen, *oldPen;
	int xs, ys, xe, ye;
	bool vert;

	GetClientRect(rect);

	if(hTheme==NULL)
		if(IsThemeActive())
			hTheme = OpenThemeData(m_hWnd, L"button");

	if(hTheme!=NULL){
		hColor = GetThemeSysColor(hTheme, COLOR_BTNHILIGHT);
		sColor = GetThemeSysColor(hTheme, COLOR_BTNSHADOW);
	}
	else{
		hColor = GetSysColor(COLOR_BTNHILIGHT);
		sColor = GetSysColor(COLOR_BTNSHADOW);
	}

	vert = rect.Height() > rect.Width();
	if(vert){
		xs = (rect.left + rect.right) / 2;
		ys = rect.top;
		xe = xs;
		ye = rect.bottom;
	}
	else{
		xs = rect.left;
		ys = (rect.top + rect.bottom) / 2;
		xe = rect.right;
		ye = ys;
	}

	pen.CreatePen(PS_SOLID, 1, sColor);
	oldPen = dc.SelectObject(&pen);
		dc.MoveTo(xs, ys);
		dc.LineTo(xe, ye);
	dc.SelectObject(oldPen);
	pen.DeleteObject();

	if(vert){
		xs++;
		xe++;
	}
	else{
		ys++;
		ye++;
	}

	pen.CreatePen(PS_SOLID, 1, hColor);
	oldPen = dc.SelectObject(&pen);
		dc.MoveTo(xs, ys);
		dc.LineTo(xe, ye);
	dc.SelectObject(oldPen);
	pen.DeleteObject();
}

//---------------------------------------------------------------------------

LRESULT MCCLine::OnThemeChanged(WPARAM, LPARAM lCount)
{
	if(hTheme!=NULL)
		CloseThemeData(hTheme);

	if(IsThemeActive()) hTheme = OpenThemeData(m_hWnd, L"button");
	else                hTheme = NULL;

	return 0;
}

//---------------------------------------------------------------------------
